﻿Imports System.Data.OleDb

Public Class list
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If (Request.QueryString("tag") <> "") Then
            populateList("", "", Request.QueryString("tag"))
            Return
        End If

        If ((Request.QueryString("sc") Is Nothing) Or (Request.QueryString("sv") Is Nothing)) Then
            populateList("", "", "")
        Else
            populateList(Request.QueryString("sc"), Request.QueryString("sv"), "")
        End If

        If Not Me.Page.User.IsInRole("Admin") Then
            gw_list.Columns.Item(5).Visible = False
        Else
            gw_list.Columns.Item(5).Visible = True
        End If


    End Sub

    Protected Sub btn_SearchByTags_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_SearchByTags.Click

        Dim tagSelected As String = rbTagList.SelectedItem.Text

        Response.Redirect("TorontoPictureslist.aspx?tag=" + tagSelected)

    End Sub

    Protected Sub btn_Search_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Search.Click

        Dim searchColumn As String = dd_Search.SelectedValue
        Dim searchValue As String = tb_Search.Text

        Response.Redirect("TorontoPictureslist.aspx?sc=" + searchColumn + "&sv=" + searchValue)

    End Sub

    Protected Sub populateList(ByVal searchColumn As String, ByVal searchValue As String, ByVal tag As String)

        If (searchValue = "''") Then
            searchValue = ""
        End If

        If (tag = "''") Then
            tag = ""
        End If

        Dim fillList As String = "SELECT DISTINCT [ID], [TorontoPictureName], [Description], [YearTaken], [Picture], [Active] FROM [PictureTag]"

        If tag <> "" Then
            fillList = fillList + " WHERE [PictureTag].[TagName] = '" + tag + "'"
        Else
            fillList = fillList + " WHERE 1 = 1 "
        End If

        If Not Me.Page.User.IsInRole("Admin") Then

            fillList = fillList + " AND Active = True"

        End If

        If (searchColumn <> "") And (searchValue <> "") Then
            fillList = fillList + " AND " + searchColumn + " LIKE '%" + searchValue + "%'"
        End If

        SqlDataSource1.SelectCommand = fillList
        gw_list.DataBind()

    End Sub

    Public Function getRating(ByVal ID As String) As String

        Dim stars As String = ""
        Dim averageRanking As String = ""
        Dim rankingCloser As Int32 = 0

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("dbDusanCS").ConnectionString)
        oleDbConn.Open()

        Dim SqlString As String = "SELECT IIF( ISNULL(AVG(Rating)), 0 , (AVG(Rating) )) FROM Rating WHERE FK_TorontoPictureID = @param1"

        Dim cmdUserID As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmdUserID.CommandType = CommandType.Text
        cmdUserID.Parameters.AddWithValue("@param1", ID)

        Dim readerUserID As OleDbDataReader = cmdUserID.ExecuteReader()

        If readerUserID.HasRows Then
            Do While readerUserID.Read()
                averageRanking = readerUserID.Item(0).ToString()
            Loop
        End If

        rankingCloser = Math.Round(Convert.ToDecimal(averageRanking))

        For value As Integer = 1 To rankingCloser
            stars = stars + "*"
        Next

        Return stars

    End Function

End Class